package com.hainabo.mgcmall.util;

import com.ctc.wstx.util.StringUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by guoshihao on 2017/12/9.
 */
public class MapUtil {


    private static String key = "3450a52ae03af206d65dc9fea97df22a";


    /**
     * 计算地球上任意两点(经纬度)距离
     *
     * @param long1
     *            第一点经度
     * @param lat1
     *            第一点纬度
     * @param long2
     *            第二点经度
     * @param lat2
     *            第二点纬度
     * @return 返回距离 单位：米
     */
    public static double Distance(double long1, double lat1, double long2,
                                  double lat2) {
        double a, b, R;
        R = 6378137; // 地球半径
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2
                * R
                * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1)
                * Math.cos(lat2) * sb2 * sb2));
        return d;
    }


    /**
     * @Title: getDistanceByPoints
     * @Description: 调用高德API接口计算两点驾车距离
     * @return 返回map
     */
    public static Map<String, Object> getDistanceByPoints(double lat1 , double lng1, double lat2,
                                                          double lng2){

        //存放返回值map
        Map<String, Object> map = new HashMap<String, Object>();

        //拼接计算两点驾车距离的高德API接口
        String url =  String.format("http://restapi.amap.com/v3/direction/driving?"+
                "origin=" + lng1+"," +lat1+"&destination="+lng2+"," + lat2 + "&output=json&key="+key);

        String json = loadJSON(url);

        //将返回结果转换成map
        Map<String, Object> reMap = JsonUtil.toHashMap(json);

        //请求成功
        if(reMap.get("status").toString().equals("1")){
            Map result = JsonUtil.toHashMap(reMap.get("route"));
            map = JsonUtil.toList(result.get("paths")).get(0);
        }
        return map;
    }


    /**
     * @Title: loadJSON
     * @Description: 得到请求返回结果
     * @param url 百度API请求地址
     * @return 返回String雷锤
     */
    public static String loadJSON (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }


}
